package livesun.justiceeternal.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.lang.reflect.Field;

import livesun.justiceeternal.R;
import livesun.justiceeternal.adapter.ItemsBean;
import livesun.justiceeternal.adapter.ScoreBean;
import livesun.justiceeternal.adapter.ScoreListAdapter;
import livesun.justiceeternal.base.BaseActivity;
import livesun.justiceeternal.base.Contract;
import livesun.justiceeternal.contract.IGetScoreContract;
import livesun.justiceeternal.persenter.GetScorePersenter;
import livesun.justiceeternal.utils.ToastUtil;
import livesun.justiceeternal.view.adapter.OnItemClickListener;

/**
 * 类描述：
 * 创建人：livesun
 * 创建时间：2018/1/3.
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */
public class GetScoreActivity extends BaseActivity implements IGetScoreContract.View, OnItemClickListener<ItemsBean> {
    private RecyclerView recycleView;
    private GetScorePersenter mScorePersenter;
    Intent mIntent;
    private ScoreListAdapter mAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    private View mEmptyView;

    @Override
    public int getResourcesId() {
        mIntent = new Intent(this, ScoreInforActivity.class);
        return R.layout.activity_getscore;
    }



    @Override
    public void initView() {
        CardView cardView=findView(R.id.search_card_view);
        cardView.setOnClickListener(v -> {
            finish();
            overridePendingTransition(0,R.anim.activity_out);

        });

        recycleView=findView(R.id.recycler);
        mRefreshLayout = findView(R.id.refresh);
        mEmptyView = findView(R.id.empet);

        mScorePersenter = new GetScorePersenter(this,this);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ScoreListAdapter(this);
        recycleView.setAdapter(mAdapter);

    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(this);
        mRefreshLayout.setOnRefreshListener(() -> {
            mScorePersenter.start();
        });

        mScorePersenter.start();
        mRefreshLayout.measure(0,0);
        mRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        mRefreshLayout.setRefreshing(true);

    }

    @Override
    public void onClick(View v, int id) {

    }

    @Override
    public void setPersenter(IGetScoreContract.Persenter persenter) {
        this.mScorePersenter = (GetScorePersenter) persenter;
    }

    @Override
    public void getDatasSucess(ScoreBean itemsBean) {
        if(itemsBean==null||itemsBean.getItems()==null||itemsBean.getItems().size()<=0){
            mEmptyView.setVisibility(View.VISIBLE);
        }else{
            mEmptyView.setVisibility(View.GONE);
            mAdapter.setDatas(itemsBean.getItems());
        }
        if(mRefreshLayout.isRefreshing()){
            mRefreshLayout.setRefreshing(false);
        }

    }

    @Override
    public void getDataError(String erro) {
        mEmptyView.setVisibility(View.VISIBLE);
        ToastUtil.showShort(erro);
    }

    @Override
    public void loadMoreSucess(ScoreBean itemsBean) {
        mAdapter.addDatas(itemsBean.getItems());
    }

    @Override
    public void loardMoreError(String erro) {
//        mAdapter.loadMoreFailed();
    }

    @Override
    public String getKeyWords() {
        return getIntent().getStringExtra("keyword");
    }


    @Override
    public void onItemClick(ItemsBean item, int position) {
        mIntent.putExtra(Contract.ITEMBEAN,item);
        startActivity(mIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mScorePersenter!=null){
            mScorePersenter.cancel();
        }
    }
}
